<?php

class Login extends BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index($any) {
        if ($any == 'sv' || $any == 'cb' || $any == 'tbm' || $any == 'qtht') {
            return View::make('common.login')
                            ->with('any', $any)
                            ->with('title', 'Đăng nhập hệ thống');
        } else {
            return Redirect::to('/');
        }
    }

    public function authen() {
        $any = Input::get('txtNhomQuyen');
        $quyen = "";
        switch ($any) {
            case 'sv': $quyen = "4";
                break;
            case 'cb': $quyen = "3";
                break;
            case 'tbm': $quyen = "2";
                break;
            case 'qtht': $quyen = "1";
                break;
        }

        $user = array(
            'tai_khoan' => Input::get('txtTaiKhoan'),
            'password' => Input::get('txtMatKhau'),
            'hoat_dong' => 1
        );

        $validation = nguoidung::validate($user);

        if ($validation->passes()) {
            if (Auth::attempt($user)) {

                $array_nhom_quyen = explode(',', Auth::user()->id_nhom_quyen);

                if (Login::recursive_array_search($quyen, $array_nhom_quyen)) {
                    Session::put('quyen',$any);
                    $this->getInforUser(Auth::user()->id, $quyen);
                    $this->getFunctionUser($quyen);
                    return Redirect::to("/" . $any);
                } else {
                    Auth::logout();
                    return Redirect::to("login/" . $any)
                                    ->with('result', false)
                                    ->withInput();
                }
            } else {
                return Redirect::to("login/" . $any)
                                ->with('result', false)
                                ->withInput();
            }
        } else {
            return Redirect::to("login/" . $any)
                            ->with('result', false)
                            ->withInput();
        }
    }

    private function getInforUser($id, $quyen) {
        switch ($quyen) {
            case '1':
            case '2':
            case '3':
                $infor = DB::table('can_bo')
                        ->join('bo_mon', function($join) {
                                    $join->on('bo_mon.id', '=', 'can_bo.id_bo_mon');
                                })
                        ->select('can_bo.mscb', 'can_bo.ho', 'can_bo.ten', 'can_bo.gioi_tinh', 'can_bo.ngay_sinh', 'can_bo.email', 'can_bo.sdt_dd', 'can_bo.sdt_cd', 'bo_mon.ten_bo_mon','can_bo.id_bo_mon','can_bo.id')
                        ->where('can_bo.id_nguoi_dung', $id)
                        ->get();
                Session::push('infor.id_nd', $infor[0]->id);
                Session::push('infor.ms', $infor[0]->mscb);
                Session::push('infor.hoten', $infor[0]->ho . " " . $infor[0]->ten);
                if (!$infor[0]->gioi_tinh)
                    Session::push('infor.gt', "Nam");
                else
                    Session::push('infor.gt', "Nữ");
                Session::push('infor.ns', date('d/m/Y', strtotime($infor[0]->ngay_sinh)));
                Session::push('infor.email', $infor[0]->email);
                Session::push('infor.bm', $infor[0]->ten_bo_mon);
                Session::push('infor.idbm', $infor[0]->id_bo_mon);
                Session::push('infor.sdt1', $infor[0]->sdt_dd);
                Session::push('infor.sdt2', $infor[0]->sdt_cd);
                break;
            case '4':
                $infor = DB::table('sinh_vien')
                        ->join('lop', function($join) {
                                    $join->on('sinh_vien.id_lop', '=', 'lop.id');
                                })
                        ->join('bo_mon', function($join) {
                                    $join->on('lop.id_bo_mon', '=', 'bo_mon.id');
                                })
                        ->select('sinh_vien.id','sinh_vien.mssv', 'sinh_vien.ho', 'sinh_vien.ten', 'sinh_vien.gioi_tinh', 'sinh_vien.ngay_sinh', 'sinh_vien.email', 'sinh_vien.sdt', 'sinh_vien.khoa', 'lop.ten_lop', 'lop.ma_so_lop', 'bo_mon.ten_bo_mon','lop.id_bo_mon')
                        ->where('sinh_vien.id_nguoi_dung', $id)
                        ->get();

                Session::push('infor.id_nd', $infor[0]->id);
                Session::push('infor.ms', $infor[0]->mssv);
                Session::push('infor.hoten', $infor[0]->ho . " " . $infor[0]->ten);
                if (!$infor[0]->gioi_tinh)
                    Session::push('infor.gt', "Nam");
                else
                    Session::push('infor.gt', "Nữ");
                Session::push('infor.ns', date('d/m/Y', strtotime($infor[0]->ngay_sinh)));
                Session::push('infor.email', $infor[0]->email);
                Session::push('infor.bm', $infor[0]->ten_bo_mon);
                Session::push('infor.idbm', $infor[0]->id_bo_mon);
                Session::push('infor.tenlop', $infor[0]->ten_lop);
                Session::push('infor.malop', $infor[0]->ma_so_lop);
                Session::push('infor.khoa', $infor[0]->khoa);
                Session::push('infor.sdt', $infor[0]->sdt);
                break;
        }
    }

    private function getFunctionUser($quyen) {
        $chuoi_cn = ChiTietNhomQuyenHan::select('id_tac_vu_ht')
                ->where('id_nhom_quyen', $quyen, 'and')
                ->where('nhom', 'main-panel')
                ->get();
        
        $array_id_cn = explode(',', $chuoi_cn);
        
        $ds_chuc_nang = DB::table('tac_vu_he_thong')
                ->select('mo_ta_menu','id','icon_menu')
                ->whereIn('id',$array_id_cn,'and')
                ->where('hoat_dong',1)
                ->get();
        //session main-panel
        foreach($ds_chuc_nang as $chuc_nang)
        {
                Session::push('mpanel.menu', $chuc_nang->mo_ta_menu);
                Session::push('mpanel.id', $chuc_nang->id);
           //     Session::push('mpanel.link', $chuc_nang->link_menu);
                Session::push('mpanel.icon', $chuc_nang->icon_menu);
        }
       
        
    }
    
    public static function getMenu($any,$panel){
        Session::forget('tmenu');
        $quyen = "";
        switch ($any) {
            case 'sv': $quyen = "4";
                break;
            case 'cb': $quyen = "3";
                break;
            case 'tbm': $quyen = "2";
                break;
            case 'qtht': $quyen = "1";
                break;
        }
         //session menu
        $chuoi_menu_cn = ChiTietNhomQuyenHan::select('id_tac_vu_ht')
                    ->where('id_nhom_quyen',$quyen,'and')
                            ->where('nhom','menu-bar')
                            ->get();
        foreach($chuoi_menu_cn as $chuoi_menu)
        {
            if(strstr($chuoi_menu->id_tac_vu_ht,'-',true) == $panel){
           Session::push('tmenu', $panel);
           $ch_menu = substr(strstr($chuoi_menu, '-'),1);
           
           $array_id_menu = explode(',', $ch_menu);
           
           $ds_chuc_nang = DB::table('tac_vu_he_thong')
                ->select('mo_ta_menu','link_menu')
                ->whereIn('id',$array_id_menu,'and')
                ->where('hoat_dong',1)
                ->get();
           foreach($ds_chuc_nang as $chuc_nang)
           {
               Session::push('tmenu.menu',$chuc_nang->mo_ta_menu);
               Session::push('tmenu.link',$chuc_nang->link_menu);
           }
            }
        }
    }

    public static function recursive_array_search($needle, $haystack) {
        foreach ($haystack as $key => $value) {
            if ($needle === $value OR (is_array($value) && recursive_array_search($needle, $value) !== false)) {
                return true;
            }
        }
        return false;
    }

    public function destroy() {
        Session::flush();
        Auth::logout();
        return Redirect::to('/');
    }

}
